package com.camel.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.camel.base.model.ZsInformation;
import com.camel.base.model.ZsTrans;
import com.camel.base.service.ZsInformationService;
import com.camel.base.service.ZsTransService;
import com.camel.base.utils.ImportUtil;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ExcelUtil;
import com.camel.core.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author camel
 */
@RestController
@RequestMapping("zsInfo")
public class ZsInformationController extends BaseCommonController<ZsInformation> {

    @Autowired
    private ZsInformationService<ZsInformation> service;

    @Autowired
    private ZsTransService<ZsTrans> zsTransService;

    /**
     * EXCEL 导入客户信息
     * @param file
     * @param sid
     * @return
     */
    @PostMapping("/imports/{sid}")
    public Result imports(MultipartFile file, @PathVariable Integer sid) {
        List<ZsTrans> list = zsTransService.list();
        Map<String, String> collect = list.stream().collect(Collectors.toMap(ZsTrans::getZh, ZsTrans::getEn));
        List<ZsInformation> zsInformations = new ImportUtil(file, sid).initTransMap(collect).readInfomations();
        for (ZsInformation zsInformation : zsInformations) {
            service.saveOrUpdate(zsInformation, new UpdateWrapper<ZsInformation>()
                    .eq("survey_id", zsInformation.getSurveyId())
                    .eq("phone", zsInformation.getPhone()));
        }
        return ResultUtil.success("操作成功");
    }

    @Override
    public boolean deleteAble(Serializable id) {
        ZsInformation byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @Override
    public BaseCommonService<ZsInformation> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "号码信息";
    }
}
